﻿using BaoVeThanhDia.GamePlay.Monsters;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace BaoVeThanhDia.GamePlay.Towers
{
    class CannonTower : Tower
    {
        private float countDraw;// biến phụ đếm thời gian
        int X2ST;
        Random rand;
        public CannonTower() { }
        public CannonTower(Vector2 index, int level, int type)
        {
            sound = new Sound.CustomSoundEffect("sounds/effect/Tower/Dai_Bac");
            ValueBuff = 0;
            countDraw = 0;
            setIndex(index);
            setLevel(level, type);
            setelement();
            weapons = new List<Weapon>();
            X2ST = 0;
            rand = new Random();
        }
        public override void Load(ContentManager content)
        {
            sound.Load(content);
            scale = 128 / 100f;
            Image = content.Load<Texture2D>("images/towers/cannon/" + Level.ToString() + (Type == 0 ? "" : Type.ToString()));
        }
        public override void setLevel(int level, int type)
        {
            Level = level;
            Type = type;
            if (Type == 2) Effect = "Stun";
            else Effect = "None";
        }
        public override void Draw(ContentManager cont, SpriteBatch sprite)
        {
            if (ValueBuff > 0)
            {
                ImageBuffed = cont.Load<Texture2D>("images/towers/soul/root" + (ValueBuff < 30 ? "" : (ValueBuff < 50 ? "1" : "2")));
                sprite.Draw(ImageBuffed, (Position + new Vector2(-13, -45)) * GamePage.Scale + GamePage.Dolech, new Rectangle(((int)countDraw / 5) % 6 * 100, 0, 100, 100), Color.White, 0f, Vector2.Zero, 1.5f * GamePage.Scale, SpriteEffects.None, 0f);
            }
            DrawWeapon(cont, sprite);
            sprite.Draw(Image, (Position + new Vector2(2, -10)) * GamePage.Scale + GamePage.Dolech, new Rectangle((int)(countDraw / 5) * 100, 0, 100, 100), Color.White, 0f, Vector2.Zero, scale * GamePage.Scale, SpriteEffects.None, 0f);
            countDraw += Speed * GamePage.Speed / 2;
            if (countDraw >= 25) countDraw = 0;
        }
        public override void Ban()
        {
            if (weapons.Count == 0)
            {
                if ((int)countDraw / 5 == 4 && (int)countDraw % 5 == 0)
                {
                    bool isX2ST = false;
                    if (X2ST == 15 && rand.Next(10000) % 100 < 15) isX2ST = true;
                    bool isEffect = false;
                    if (rand.Next(10000) % 100 < countEffect) isEffect = true;
                    for (int i = 0; i < GamePage.monsters.Count; i++)
                    {
                        if (Vector2.Distance(Position, GamePage.monsters[i].getPosition()) <= Range * 128 && GamePage.monsters[i].getCurHP() > 0)
                        {
                            sound.Play();
                            weapons.Add(new Weapon(2, PhysicDame * (isX2ST ? 2 : 1) * (isEffect && Type == 1 ? 3 : 1) * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (isX2ST ? 2 : 1) * (isEffect && Type == 1 ? 3 : 1) * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 30, Position + new Vector2(64, 0) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, (isEffect ? Effect : "None"), (Level == 3 ? 30 : 60), 0));
                            break;
                        }
                    }
                }
            }
        }
        public override void setelement()
        {
            if (Level == 1)
            {
                Price = 100;
                PriceBought = 100;
                PhysicDame = 30;
                MagicDame = 3;
                Speed = 0.5f;
                Range = 2f;
                countEffect = 0;
            }
            else if (Level == 2)
            {
                Price = 50;
                PriceBought = 150;
                PhysicDame = 40;
                MagicDame = 4;
                Speed = 0.5f;
                Range = 2.16f;
                countEffect = 0;
            }
            else if (Level == 3)
            {
                Price = 60;
                PriceBought = 210;
                Range = 2.33f;
                PhysicDame = 50;
                MagicDame = 5;
                if (Type == 1)
                {
                    Speed = 0.7f;
                    countEffect = 15;
                }
                else
                {
                    Speed = 0.6f;
                    countEffect = 15;
                }
            }
            else
            {
                Price = 70;
                PriceBought = 280;
                Range = 2.5f;
                PhysicDame = 70;
                MagicDame = 6;
                Speed = 0.5f;
                if (Type == 1)
                {
                    countEffect = 30;
                }
                else
                {
                    countEffect = 20;
                }
            }
            getUpGradeValue();
        } // dành riêng cho tháp pháo
        public override void OptionDraw(SpriteBatch sprite, Vector2 posi, Vector2 dolech, float scale, bool isTouch)
        {
            sprite.Draw(Image, posi + dolech, new Rectangle((int)(countDraw / 5) * 100, 0, 100, 100), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            if (isTouch)
            {
                countDraw += Speed;
                if (countDraw >= 25) countDraw = 0;
            }
        }
        private void getUpGradeValue()
        {
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[16])
            {
                X2ST = 15;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[17])
            {
                MagicDame += (MagicDame * 3) / 20;
                PhysicDame += (PhysicDame * 3) / 20;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[18])
            {
                Speed += (Speed * 3) / 20;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[19])
            {
                if (Type == 2)
                    countEffect += 15;
            }
        }
        public override float getPriceUpGrade(int level, int type)
        {
            return new CannonTower(Vector2.Zero, level, type).Price;
        }
        public override void DrawPictureUpGrade(int level, int type, ContentManager cont, SpriteBatch sprite, Vector2 posi)
        {
            CannonTower temp = new CannonTower(Vector2.Zero, level, type);
            temp.Load(cont);
            temp.OptionDraw(sprite, posi + new Vector2(-4, -4) * GamePage.Scale, GamePage.Dolech, 1f * GamePage.Scale, false);
        }
    }
}
